Object.prototype.getType = function() { 
   var funcNameRegex = /function (.{1,})\(/;
   var results = (funcNameRegex).exec((this).constructor.toString());
   return (results && results.length > 1) ? results[1] : "";
};


var composeController={
	url:"https://www.pennytel.com/pennytelapi/services/PennyTelAPI",
	maxCharLimit:150,
	controller:null,
	connected:null,
	userData:null,
    timer:null,
    txtNumber:null,
    txtMessage:null,
    txtCharsLeft:null,
    txtFeedback:null,
    drpAddressBook:null,
    availableCredit:null,
	initialize:function(userData){	
		composeController.txtNumber = document.getElementById("ap-number");
		composeController.txtMessage = document.getElementById("ap-message");
		composeController.drpAddressBook = document.getElementById("ap-contact");
		composeController.txtCharsLeft = document.getElementById("ap-charleft");
		composeController.txtFeedback = document.getElementById("ap-feedback");
		
		composeController.txtMessage.addEventListener("keydown",composeController.CharCounter,false);
		composeController.txtMessage.addEventListener("keyup",composeController.CharCounter,false);
		composeController.UpdateCharCounter(composeController.maxCharLimit);
		
		if (userData.userName!=null && userData.password!=null)
		{
			composeController.userData=userData;
			
			composeController.LoadAddressBook();
			composeController.LoadMessageDetails();
		}
		else
		{
			document.getElementById("loaderImage").hidden=true;
			/*composeController.txtFeedback.value = "Setup configuration first";
			composeController.txtFeedback.className="error";*/
			UIcontroller.onConfiguration();
		}
	},
	UpdateCharCounter:function(value)
	{
		composeController.txtCharsLeft.value=value + " chars left";
	},
	CharCounter:function()
	{
		if (this.value.length > composeController.maxCharLimit)
			this.value = this.value.substring(0, composeController.maxCharLimit);
		else
			composeController.UpdateCharCounter(composeController.maxCharLimit - this.value.length);

	},
	LoadMessageDetails:function()
	{
		document.getElementById("loaderImage").hidden=false;
		var paramArray = new SOAPClientParameters();
		paramArray.add("ID",composeController.userData.userName); 
		paramArray.add("Password",composeController.userData.password); 
		SOAPClient.invoke(composeController.url, "getAccount", paramArray, true,composeController.populateAccountDetails);
		
	},
	populateAccountDetails:function(o,response)
	{
		document.getElementById("loaderImage").hidden=true;
		if (o.getType()=="Error")
		{
			Components.utils.reportError("Pennyel service failure - " + o.fileName);
			composeController.connected = false;
			composeController.txtFeedback.value = "Could not connect, try later";
			
		}
		else
		{
			var details = o.getAccountReturn.others;
			var otherArray = details.split(";");
			
			composeController.availableCredit = parseInt(otherArray[3].split("=")[1]) - parseInt(otherArray[2].split("=")[1]);
			
			composeController.connected = true;
			composeController.txtFeedback.value = "Connected - Free SMS:" + composeController.availableCredit;
		}
	},
	LoadAddressBook:function()
	{
		var paramArray = new SOAPClientParameters();
		paramArray.add("ID", composeController.userData.userName); 
		paramArray.add("Password",composeController.userData.password); 
		paramArray.add("Criteria","%"); 
		
		SOAPClient.invoke(composeController.url, "getAddressBookEntries", paramArray, true,composeController.populateAddressBook)
	},
	populateAddressBook:function(contacts,response)
	{
		if (contacts.getType()=="Error")
		{
			Components.utils.reportError("Pennyel service failure - " + contacts.fileName);
		}
		else
		{
			var menuList = composeController.drpAddressBook;
			menuList.removeAllItems();
			menuList.appendItem("","");
			for (var i = 0; i < contacts.length; i++) 
			{
				if (contacts[i].displayName!="")
				{
						menuList.appendItem(contacts[i].displayName, contacts[i].mobileNumber);
				}
			}
		}
	}
	,
	sendMessage:function(){
		if (composeController.connected) {
			var number = composeController.drpAddressBook.value;
			if (number == "") {
				var number = composeController.txtNumber.value;
			}
			var message = composeController.txtMessage.value;
			
			number = number.replace("+", "");
			
			var username = composeController.userData.userName;
			var password = composeController.userData.password;
			
			
			var paramArray = new SOAPClientParameters();
			paramArray.add("ID",username); 
			paramArray.add("Password",password); 
			paramArray.add("Type",1); 
			paramArray.add("To",number); 
			paramArray.add("Message",message); 
			paramArray.add("Date","2001-10-26T21:32:52"); 
			SOAPClient.invoke(composeController.url, "sendSMS", paramArray, true,composeController.onMessageSent);
			composeController.txtFeedback.value="Sending Message";
			document.getElementById("loaderImage").hidden=false;
			
		}
		return false;
	},
	onMessageSent:function(o,response){
        document.getElementById("loaderImage").hidden=true;
        if (response.fault)
        {
            composeController.txtFeedback.value=response.fault.faultCode;
        }
        else
        {
            composeController.txtFeedback.value="Message Sent";
            composeController.txtMessage.value="";
            composeController.drpAddressBook.selectedIndex=0;
            composeController.txtNumber.value="";
        }
        setTimeout (composeController.reloadCredits, 4000 );
	},
	reloadCredits:function()
	{
		composeController.availableCredit--
		composeController.txtFeedback.value = "Connected - Free SMS:" + composeController.availableCredit;
	}
	
	
}

composeController.initialize(userData);
